<template>
  <el-dialog
    title="修改学生信息"
    :visible.sync="dialogVisible"
    width="500px">
    <el-form label-width="100px" :model="formData" ref="form" :rules="rules">
      <el-form-item label="学号" prop="id">
        <el-input v-model="formData.id" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="formData.gender">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生年月" prop="birthday">
        <el-date-picker
          v-model="formData.birthday"
          value-format="yyyy-MM-dd"
          type="date"
          size="medium"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-input v-model="formData.grade" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="学院" prop="department">
        <el-input v-model="formData.department" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-input v-model="formData.major" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="classNum">
        <el-input v-model="formData.classNum" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="formData.phone" class="commonStyle" size="medium"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" class="commonStyle" size="medium"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="danger" size="medium" @click="cancel">取消</el-button>
      <el-button type="primary" size="medium" @click="update">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { updateStudent } from '@/api/userInfoManage'

export default {
  name: "index",
  data() {
    return {
      // 控制弹窗
      dialogVisible: false,
      // 表单中的信息
      formData: {

      },
      rules: {
        id: [
          { required: true, message: '请输入id', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: '请选择出生年月', trigger: 'blur' }
        ],
        grade: [
          { required: true, message: '请输入年级', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请输入学院', trigger: 'blur' }
        ],
        major: [
          { required: true, message: '请输入专业', trigger: 'blur' }
        ],
        classNum: [
          { required: true, message: '请输入班级', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 打开弹框
    openDialog(val) {
      this.formData = {...val}
      this.dialogVisible = true
    },
    // 取消
    cancel() {
      this.initData()
      this.dialogVisible = false
    },
    // 初始化数据
    initData() {
      this.formData = {

      }
    },
    // 更新或新增
    async update() {
      this.$refs.form.validate(async (valid) => {
        if(!valid) {
          return false
        } else {
          const res = await updateStudent(this.formData)
          if (res.success == true) {
            this.$message.success(res.data)
            this.$emit('reloadData')
            this.dialogVisible = false
          } else {
            this.$message.error(res.errorMsg)
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="sass">
.commonStyle
  width: 200px
</style>
